package net.entelijan.cobean.examples.showcase.bind.combobox;

import static org.junit.Assert.assertEquals;

import java.util.List;

import net.entelijan.cobean.data.literature.LitBook;
import net.entelijan.cobean.examples.showcase.ShowcaseTestFrame;
import net.entelijan.cobean.examples.util.ExamplesUtil;

import org.fest.swing.fixture.FrameFixture;
import org.fest.swing.fixture.JButtonFixture;
import org.fest.swing.fixture.JComboBoxFixture;
import org.junit.Test;

public class ComboBoxFestTestCase {

	@Test
	public void testInit() {
		ComboBoxPanel panel = new ComboBoxPanel();
		ShowcaseTestFrame frame = new ShowcaseTestFrame(panel);
		FrameFixture frameFix = new FrameFixture(frame);
		try {
			frameFix.show();
			ComboBoxShowcase sc = new ComboBoxShowcase();
			sc.setService(ExamplesUtil.createTestService());
			sc.init(panel);
			JComboBoxFixture comboBoxFix = frameFix.comboBox("comboBox");
			String[] contents = comboBoxFix.contents();
			assertEquals("Ad ora incerta 1984 Primo Levi", contents[0]);
			assertEquals("A Bend in the River 1979 V.S. Naipaul", contents[3]);
			assertEquals(30, contents.length);
			comboBoxFix.requireNoSelection();
			List<LitBook> books = sc.getModel().getBooks();
			assertEquals(30, books.size());
			assertEquals("Ad ora incerta", books.get(0).getTitle());
			assertEquals("Primo Levi", books.get(0).getAuthor().getName());
			assertEquals(1979, books.get(3).getYear().intValue());
			assertEquals(null, sc.getModel().getSelectedBook());

		} finally {
			frameFix.cleanUp();
		}

	}

	@Test
	public void testSelectFromGUI() {
		ComboBoxPanel panel = new ComboBoxPanel();
		ShowcaseTestFrame frame = new ShowcaseTestFrame(panel);
		FrameFixture frameFix = new FrameFixture(frame);
		try {
			frameFix.show();
			ComboBoxShowcase sc = new ComboBoxShowcase();
			sc.setService(ExamplesUtil.createTestService());
			sc.init(panel);
			JComboBoxFixture comboBoxFix = frameFix.comboBox("comboBox");

			comboBoxFix.selectItem(3);
			pause(10);
			assertEquals("A Bend in the River", sc.getModel().getSelectedBook()
					.getTitle());
			assertEquals("V.S. Naipaul", sc.getModel().getSelectedBook()
					.getAuthor().getName());
		} finally {
			frameFix.cleanUp();
		}

	}

	@Test
	public void testSelectFromModel() {
		ComboBoxPanel panel = new ComboBoxPanel();
		ShowcaseTestFrame frame = new ShowcaseTestFrame(panel);
		FrameFixture frameFix = new FrameFixture(frame);
		try {
			frameFix.show();
			ComboBoxShowcase sc = new ComboBoxShowcase();
			sc.setService(ExamplesUtil.createTestService());
			sc.init(panel);
			JComboBoxFixture comboBoxFix = frameFix.comboBox("comboBox");

			ComboBoxShowcaseModel model = sc.getModel();
			LitBook litBook = model.getBooks().get(3);
			model.setSelectedBook(litBook);
			pause(10);
			comboBoxFix
					.requireSelection("A Bend in the River 1979 V.S. Naipaul");

		} finally {
			frameFix.cleanUp();
		}

	}

	@Test
	public void testSelectFromButton() {
		ComboBoxPanel panel = new ComboBoxPanel();
		ShowcaseTestFrame frame = new ShowcaseTestFrame(panel);
		FrameFixture frameFix = new FrameFixture(frame);
		try {
			frameFix.show();
			ComboBoxShowcase sc = new ComboBoxShowcase();
			sc.setService(ExamplesUtil.createTestService());
			sc.init(panel);
			JComboBoxFixture comboBoxFix = frameFix.comboBox("comboBox");

			JButtonFixture select0ButtonFix = frameFix.button("select0Button");
			JButtonFixture select3ButtonFix = frameFix.button("select3Button");

			select0ButtonFix.click();
			pause(10);
			comboBoxFix.requireSelection("Ad ora incerta 1984 Primo Levi");
			select3ButtonFix.click();
			pause(10);
			comboBoxFix.requireSelection("La chiave a stella 1978 Primo Levi");
			select0ButtonFix.click();
			pause(10);
			comboBoxFix.requireSelection("Ad ora incerta 1984 Primo Levi");

		} finally {
			frameFix.cleanUp();
		}

	}

	@Test
	public void testRestriction() {
		ComboBoxPanel panel = new ComboBoxPanel();
		ShowcaseTestFrame frame = new ShowcaseTestFrame(panel);
		FrameFixture frameFix = new FrameFixture(frame);
		try {
			frameFix.show();
			ComboBoxShowcase sc = new ComboBoxShowcase();
			sc.setService(ExamplesUtil.createTestService());
			sc.init(panel);
			JComboBoxFixture comboBoxFix = frameFix.comboBox("comboBox");

			JButtonFixture select0ButtonFix = frameFix.button("select0Button");
			JButtonFixture select3ButtonFix = frameFix.button("select3Button");
			JButtonFixture restrictedButtonFix = frameFix
					.button("setRestrictedButton");

			restrictedButtonFix.click();
			pause(10);

			String[] contents = comboBoxFix.contents();

			assertEquals("The Scream 2008 Rohinton Mistry", contents[0]);
			assertEquals("A Fine Balance 1996 Rohinton Mistry", contents[3]);
			assertEquals(13, contents.length);

			ComboBoxShowcaseModel model = sc.getModel();
			List<LitBook> books = model.getBooks();

			assertEquals(13, books.size());

			select0ButtonFix.click();
			pause(10);
			comboBoxFix.requireSelection("The Scream 2008 Rohinton Mistry");

			select3ButtonFix.click();
			pause(10);
			comboBoxFix
					.requireSelection("Such a Long Journey 1991 Rohinton Mistry");

		} finally {
			frameFix.cleanUp();
		}

	}

	@Test
	public void testRestrictionAndAll() {
		ComboBoxPanel panel = new ComboBoxPanel();
		ShowcaseTestFrame frame = new ShowcaseTestFrame(panel);
		FrameFixture frameFix = new FrameFixture(frame);
		try {
			frameFix.show();
			ComboBoxShowcase sc = new ComboBoxShowcase();
			sc.setService(ExamplesUtil.createTestService());
			sc.init(panel);
			JComboBoxFixture comboBoxFix = frameFix.comboBox("comboBox");

			JButtonFixture select0ButtonFix = frameFix.button("select0Button");
			JButtonFixture select3ButtonFix = frameFix.button("select3Button");
			JButtonFixture restrictedButtonFix = frameFix
					.button("setRestrictedButton");
			JButtonFixture setAllButtonFix = frameFix.button("setAllButton");

			restrictedButtonFix.click();
			pause(10);

			setAllButtonFix.click();
			pause(10);

			String[] contents = comboBoxFix.contents();

			assertEquals("Ad ora incerta 1984 Primo Levi", contents[0]);
			assertEquals("A Bend in the River 1979 V.S. Naipaul", contents[3]);
			assertEquals(30, contents.length);

			ComboBoxShowcaseModel model = sc.getModel();
			List<LitBook> books = model.getBooks();

			assertEquals(30, books.size());
			assertEquals("Ad ora incerta", books.get(0).getTitle());
			assertEquals("A Bend in the River", books.get(3).getTitle());

			select0ButtonFix.click();
			pause(10);
			comboBoxFix.requireSelection("Ad ora incerta 1984 Primo Levi");
			assertEquals("Ad ora incerta", model.getSelectedBook().getTitle());

			select3ButtonFix.click();
			pause(10);
			comboBoxFix.requireSelection("La chiave a stella 1978 Primo Levi");
			assertEquals("La chiave a stella", model.getSelectedBook()
					.getTitle());

		} finally {
			frameFix.cleanUp();
		}

	}

	private synchronized void pause(int timeMs) {
		try {
			wait(timeMs);
		} catch (InterruptedException e) {
			// Do not mind
		}

	}

}
